package com.ruoyi.prevention.worker.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.prevention.worker.mapper.WorkerMapper;
import com.ruoyi.prevention.worker.domain.Worker;
import com.ruoyi.prevention.worker.service.IWorkerService;

/**
 * 检测人员信息Service业务层处理
 * 
 * @author wad
 * @date 2022-04-06
 */
@Service
public class WorkerServiceImpl implements IWorkerService 
{
    @Autowired
    private WorkerMapper workerMapper;

    /**
     * 查询检测人员信息
     * 
     * @param workerId 检测人员信息主键
     * @return 检测人员信息
     */
    @Override
    public Worker selectWorkerByWorkerId(Long workerId)
    {
        return workerMapper.selectWorkerByWorkerId(workerId);
    }

    /**
     * 查询检测人员信息列表
     * 
     * @param worker 检测人员信息
     * @return 检测人员信息
     */
    @Override
    public List<Worker> selectWorkerList(Worker worker)
    {
        return workerMapper.selectWorkerList(worker);
    }

    /**
     * 新增检测人员信息
     * 
     * @param worker 检测人员信息
     * @return 结果
     */
    @Override
    public int insertWorker(Worker worker)
    {
        return workerMapper.insertWorker(worker);
    }

    /**
     * 修改检测人员信息
     * 
     * @param worker 检测人员信息
     * @return 结果
     */
    @Override
    public int updateWorker(Worker worker)
    {
        return workerMapper.updateWorker(worker);
    }

    /**
     * 批量删除检测人员信息
     * 
     * @param workerIds 需要删除的检测人员信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkerByWorkerIds(Long[] workerIds)
    {
        return workerMapper.deleteWorkerByWorkerIds(workerIds);
    }

    /**
     * 删除检测人员信息信息
     * 
     * @param workerId 检测人员信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkerByWorkerId(Long workerId)
    {
        return workerMapper.deleteWorkerByWorkerId(workerId);
    }

    @Override
    public String importUser(List<Worker> userList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Worker user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                Worker u = workerMapper.selectWorkerByWorkerName(user.getWorkerName());
                if (StringUtils.isNull(u))
                {
                    user.setCreateBy(operName);
                    user.setWorkerName(user.getWorkerName());
                    workerMapper.insertImportWorker(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getWorkerName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    user.setUpdateBy(operName);
                    this.updateWorker(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getWorkerName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " +user.getWorkerName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getWorkerName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
